package com.example.pixelyear.ui.act

import android.os.Bundle
import com.example.pixelyear.R
import com.example.pixelyear.adapter.BaseAdapter
import com.example.pixelyear.adapter.ColorLandscapeAdapter
import com.example.pixelyear.adapter.MoodAdapter
import com.example.pixelyear.bean.ColorBean
import com.example.pixelyear.bean.DateBean
import com.example.pixelyear.bean.StringBean
import com.example.pixelyear.sql.ColorSqlUtils
import com.example.pixelyear.sql.NoteSqlUtils
import com.example.pixelyear.sql.RecordSqlUtils
import com.example.pixelyear.ui.base.BaseActivity
import com.example.pixelyear.utils.Constant
import com.example.pixelyear.utils.UIUtils
import com.example.pixelyear.utils.Utils
import com.example.pixelyear.utils.YxsDisplay
import com.example.pixelyear.utils.extension.horizontalManager
import com.example.pixelyear.utils.extension.setOnDoubleClickListener
import com.example.pixelyear.utils.extension.stringText
import com.socks.library.KLog
import kotlinx.android.synthetic.main.activity_note.*

class NoteActivity : BaseActivity() {
    private var year = 0
    private var month = 0
    private var day = 0
    private var color = 0
    private var mood = ""
    private var isEmpty = true

    private val moodArray = UIUtils.getStringArray(R.array.mood) as MutableList<String>

    private var defaultMood = moodArray[0]

    private val noteSql by lazy {
        NoteSqlUtils.getInstance(mActivity)
    }

    private val colorSql by lazy {
        ColorSqlUtils.getInstance(mActivity)
    }

    private val data by lazy {
        colorSql?.getData()
    }

    private val mAdapter by lazy {
        ColorLandscapeAdapter(mActivity)
    }
    private val moodAdapter by lazy {
        MoodAdapter(mActivity)
    }

    override fun getLayoutResID(): Int {
        return R.layout.activity_note
    }

    override fun initView(bundle: Bundle?) {
        Note_Recycler.horizontalManager()
        Note_Recycler.adapter = mAdapter
        Note_MoodRecycler.horizontalManager()
        Note_MoodRecycler.adapter = moodAdapter
    }

    override fun initData() {
        data?.let { mAdapter.updateList(it) }
        moodAdapter.updateList(moodArray)

        with(intent?.getBundleExtra(YxsDisplay.beanName)) {
            if (this != null) {
                year = getInt("y")
                month = getInt("m")
                day = getInt("d")
                color = getInt("c")
                mood = getString("mood") ?: ""
                Note_Date.text = "$year 年 $month 月 $day 日"
            }
        }

        with(noteSql?.getData("$year-$month-$day")) {
            if (this != null) {
                Note_Edit.setText(this.data)
                isEmpty = false
            }
        }

    }

    override fun initListener() {

        mAdapter.setOnItemClickListener(object : BaseAdapter.OnItemClick<ColorBean> {
            override fun click(bean: ColorBean, position: Int) {
                KLog.e("yxs", "更新颜色：${bean.color}===${bean.text}")
                Note_Text.text = bean.text
                Note_Text.setTextColor(bean.color)
            }
        })

        Note_Save.setOnDoubleClickListener {
            val color = mAdapter.getItemAtPosition(mAdapter.nowChoose).color
            RecordSqlUtils.getInstance(mActivity)
                ?.addData(
                    //Color.parseColor("#00FFFFFF") xor 取消取对比色
                    DateBean(
                        month, year, day, color,
                        Utils.colorDepth(color), defaultMood
                    )
                )

            if (Note_Edit.stringText().isNotEmpty()) {
                noteSql?.addNote(StringBean("$year-$month-$day", Note_Edit.stringText()))
            } else {
                if (!isEmpty) {
                    noteSql?.deleteNote("$year-$month-$day")
                }
            }

            setResult(Constant.EDIT_NOTE_RESULT)
            finish()
        }

        moodAdapter.setOnItemClickListener(object : BaseAdapter.OnItemClick<String> {
            override fun click(bean: String, position: Int) {
                defaultMood = bean
            }
        })

        updateColor()
    }


    private fun updateColor() {
        val index = data?.map { it.color }?.indexOf(color).let {
            if (it == null) {
                0
            } else {
                if (it < 0)
                    0
                else it
            }

        }
        mAdapter.updatePosition(index, mAdapter.getItemAtPosition(index))

        val moodIndex = moodArray.indexOf(mood)
        KLog.e("yxs", "获取的索引：$moodIndex")
        if (moodIndex >= 0)
            moodAdapter.updatePosition(moodIndex, moodArray[moodIndex])
    }
}
